package com.ma.logica;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.RotateToAction;
import com.badlogic.gdx.utils.Array;
import com.ma.gestureDetectors.AsteroideGestureDetector;
import com.ma.table.LogicaScreenOne;
import com.ma.table.actors.Asteroide;
import com.ma.table.actors.Coin;

public class Spawns {

	private LogicaScreenOne logSO;

	public Spawns(LogicaScreenOne logSO) {
		this.logSO = logSO;
	}

	public Array<Asteroide> spawnAsteroide(Array<Asteroide> asteroides) {
		Asteroide asteroide = new Asteroide();
		RotateToAction rotate = new RotateToAction();

		if (MathUtils.random(1, 10) == 2) {
			asteroide.setMultiHit(true);
			asteroide.setHits(10);
			asteroide.setHeight(Constantes.TAMANIO_ASTEROIDE__GRANDE_H);
			asteroide.setWidth(Constantes.TAMANIO_ASTEROIDE_GRANDE_W);
			asteroide.setVelocidad(MathUtils.random(20, 150));
			rotate.setRotation(MathUtils.random(-20000, 20000));
			rotate.setDuration(MathUtils.random(60, 200));
			asteroide.setOrigin(asteroide.getHeight() / 2,asteroide.getWidth() / 2);
		} else {
			asteroide.setMultiHit(false);
			asteroide.setHits(1);
			asteroide.setHeight(Constantes.TAMANIO_ASTEROIDE_H);
			asteroide.setWidth(Constantes.TAMANIO_ASTEROIDE_W);
			asteroide.setVelocidad(MathUtils.random(20, 350));
			rotate.setRotation(MathUtils.random(-200000, 200000));
			rotate.setDuration(MathUtils.random(150, 2000));
			asteroide.setOrigin(MathUtils.random(0, asteroide.getX()),
					MathUtils.random(0, asteroide.getY()));
		}

		asteroide.addAction(rotate);

		float randomX = MathUtils.random(0,(LogicaJuego.w - asteroide.getWidth()));
		asteroide.setX(randomX);
		asteroide.setY(LogicaJuego.h + asteroide.getHeight());

		// asteroide.setVelocidad(MathUtils.random(20,20)*Gdx.graphics.getDeltaTime());
		logSO.addActor(asteroide);
		asteroide.addListener(new AsteroideGestureDetector(logSO));
		asteroides.add(asteroide);
		return asteroides;
	}

	public Array<Coin> spawnCoins(Array<Coin> coins, Asteroide asteroide, boolean diamante) {
		MoveToAction move = new MoveToAction();
		move.setPosition(Constantes.POSICON_X_COIN, Constantes.POSICON_Y_COIN);
		
		move.setDuration(MathUtils.random(0.5f, 2f));

		Coin coin = new Coin();
		coin.addAction(move);
		coin.setHeight(Constantes.TAMANIO_COIN_H);
		coin.setWidth(Constantes.TAMANIO_COIN_W);
		coin.setDiamante(diamante);
		coin.setX(MathUtils.random(10, 70)+asteroide.getX());
		coin.setY(MathUtils.random(10, 70)+asteroide.getY());
		logSO.addActor(coin);
		coins.add(coin);
		return coins;
	}
}
